'use strict';
/**
 * @param {Egg.Application} app - egg application
 */
const { list } = require('./list.json');

module.exports = app => {
  const { router, controller } = app;
  const { home, file, generator, sys } = controller;

  const perfix = '/api';

  const url = item => {
    return perfix + '/' + item.replace(/\./g, '/');
  };

  const ctrl = item => {
    const path = item.split('.');
    let temp = {};
    path.forEach((element, index) => {
      temp = index === 0 ? controller[element] : temp[element];
    });
    return temp;
  };

  // default
  router.get('/', home.index);
  router.post(`${perfix}/login`, sys.user.login);
  router.post(`${perfix}/logout`, sys.user.logout);
  router.post(`${perfix}/file/upload`, file.upload);
  router.resources('generator', '/api/generator', generator.g);
  // sys
  router.resources('sys.user', `${perfix}/sys/user`, sys.user);
  router.resources('sys.role', `${perfix}/sys/role`, sys.role);
  router.resources('sys.permission', `${perfix}/sys/permission`, sys.permission);
  router.resources('sys.resource', `${perfix}/sys/resource`, sys.resource);
  // auto
  list.map(item => {
    return router.resources(item, url(item), ctrl(item));
  });

  // customer
  // router.resources('xxx', '/api/xxx', xx);
};
